package hichecker.university;

import hichecker.university.jaxb.HashMapAdapter;

import java.util.HashMap;
import java.util.UUID;
import java.util.Map.Entry;

import javax.xml.bind.annotation.XmlList;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

/**
 * Class representing a series related to a {@link PTL}
 */
@XmlType(propOrder = {"grades", "gradingKey"})
public class PTLSeries extends Series {
	private @XmlJavaTypeAdapter(HashMapAdapter.class) HashMap<Student, Float> grades;
	private @XmlList int[] gradingKey;

	/**
	 * Creates a new ptl series
	 */
	public PTLSeries() {
	}
	
	/**
	 * Creates a new ptl series with a referenced id
	 * 
	 * @param id
	 */
	public PTLSeries(UUID id) {
		setID(id);
	}
	
	/**
	 * Creates a new ptl series and copies all values out of the referenced ptl series
	 * 
	 * @param ptl
	 */
	public PTLSeries(PTLSeries series) {
		super(series);
		
		grades = new HashMap<Student, Float>();
		for (Entry<Student, Float> e : series.getGrades().entrySet())
			grades.put(e.getKey(), e.getValue());
		
		if (series.getGradingKey() == null) return;
		gradingKey = new int[series.getGradingKey().length];
		for (int i = 0; i < series.getGradingKey().length; i++)
			gradingKey[i] = series.getGradingKey()[i];
	}
	
	/**
	 * @return the grades of all students
	 */
	@XmlTransient
	public HashMap<Student, Float> getGrades() {
		return grades = (grades == null ? new HashMap<Student, Float>() : grades);
	}
	
	/**
	 * Returns the grade of the referenced student
	 * 
	 * @param student
	 * @return -1 if the student didn't gain a grade yet
	 */
	public float getGrade(Student student) {
		if (grades == null) grades = new HashMap<Student, Float>();
		return grades.get(student) == null ? -1 : grades.get(student);
	}

	/**
	 * @param grades the grades of all students to set
	 */
	public void setGrades(HashMap<Student, Float> grades) {
		this.grades = grades;
	}
	
	/**
	 * @param student
	 *            the student the grade belongs to
	 * @param grade
	 *            the grade to set
	 */
	public void setGrade(Student student, float grade) {
		if (grades == null) grades = new HashMap<Student, Float>();
		grades.put(student, grade);
	}

	/**
	 * @return the grading key for alle grades
	 */
	@XmlTransient
	public int[] getGradingKey() {
		return gradingKey;
	}

	/**
	 * @param gradingKey
	 *            the grading key to set
	 */
	public void setGradingKey(int[] gradingKey) {
		this.gradingKey = gradingKey;
	}
}
